<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名字" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--el-form-item label="父ID" prop="parentId">
        <el-input
          v-model="queryParams.parentId"
          placeholder="请输入父ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="祖级列表" prop="ancestors">
        <el-input
          v-model="queryParams.ancestors"
          placeholder="请输入祖级列表"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="显示顺序" prop="orderNum">
        <el-input
          v-model="queryParams.orderNum"
          placeholder="请输入显示顺序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item-->
      <el-form-item label="级别标记" prop="levelMark">
        <el-select v-model="queryParams.levelMark" placeholder="请选择级别标记" clearable>
          <el-option
            v-for="dict in dict.type.level_mark"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="仓库类型" prop="warehouseType">
        <el-select v-model="queryParams.warehouseType" placeholder="请选择仓库类型" clearable>
          <el-option
            v-for="dict in dict.type.warehouse_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="货物类型" prop="goodsType">
        <el-select v-model="queryParams.goodsType" placeholder="请选择货物类型" clearable>
          <el-option
            v-for="dict in dict.type.goods_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="使用状态" prop="useStatus">
        <el-select v-model="queryParams.useStatus" placeholder="请选择使用状态" clearable>
          <el-option
            v-for="dict in dict.type.use_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>
      <!--el-form-item label="编码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="3PL编码" prop="threeplCode">
        <el-input
          v-model="queryParams.threeplCode"
          placeholder="请输入3PL编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="天车操作" prop="craneOperateMode">
        <el-select v-model="queryParams.craneOperateMode" placeholder="请选择天车操作" clearable>
          <el-option
            v-for="dict in dict.type.crane_operate_mode"
            :key="dict.value"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="消防工艺" prop="fireControl">
        <el-select v-model="queryParams.fireControl" placeholder="请选择消防工艺" clearable>
          <el-option
            v-for="dict in dict.type.fire_control"
            :key="dict.value"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="物联标记" prop="iotMark">
        <el-select v-model="queryParams.iotMark" placeholder="请选择物联标记" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="长度" prop="length">
        <el-input
          v-model="queryParams.length"
          placeholder="请输入长度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="宽度" prop="width">
        <el-input
          v-model="queryParams.width"
          placeholder="请输入宽度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="高度" prop="hight">
        <el-input
          v-model="queryParams.hight"
          placeholder="请输入高度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="面积" prop="area">
        <el-input
          v-model="queryParams.area"
          placeholder="请输入面积"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item-->
      <el-form-item label="地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--el-form-item label="备注" prop="remark">
        <el-input
          v-model="queryParams.remark"
          placeholder="请输入备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item-->
      <el-form-item label="创建者" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 205px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['locates:locate:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['locates:locate:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['locates:locate:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['locates:locate:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5" v-if="level > 0">
        <el-button
          type="warning"
          plain
          icon="el-icon-close"
          size="mini"
          @click="handleClose"
        >关闭</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" @computeTable="computeTableHeight"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="locateList" @selection-change="handleSelectionChange" border :height="tableHeight" :row-style="{height:'35px'}" :cell-style="{padding:'0px'}">
      <el-table-column type="selection" width="45" align="center" />
      <el-table-column type="index" prop="" width="55" label="序号" align="center" >
        <template slot-scope="scope">
          <span>{{ scope.$index+1 +(queryParams.pageNum-1)*queryParams.pageSize }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['locates:locate:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['locates:locate:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-setting"
            @click="handleWarehousManage(scope.row)" v-if="level == 0"
          >仓库管理</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-setting"
            @click="handleRegionManage(scope.row)" v-if="level == 1"
          >区位管理</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-setting"
            @click="handleGoodsLocateManage(scope.row)" v-if="level == 2"
          >货位管理</el-button>
        </template>
      </el-table-column>
      <!--el-table-column label="定位ID" align="center" prop="LocateId" /-->
      <el-table-column label="名字" align="center" prop="name" />
      <el-table-column label="父ID" align="center" prop="parentId" />
      <el-table-column label="祖级列表" align="center" prop="ancestors" width="160" :show-overflow-tooltip="true" />
      <el-table-column label="显示顺序" align="center" prop="orderNum" />
      <el-table-column label="级别标记" align="center" prop="levelMark">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.level_mark" :value="scope.row.levelMark"/>
        </template>
      </el-table-column>
      <el-table-column label="仓库类型" align="center" prop="warehouseType" />
      <el-table-column label="货物类型" align="center" prop="goodsType" />
      <el-table-column label="使用状态" align="center" prop="useStatus" />
      <el-table-column label="编码" align="center" prop="code" />
      <!--el-table-column label="3PL编码" align="center" prop="threeplCode" />
      <el-table-column label="天车操作" align="center" prop="craneOperateMode" />
      <el-table-column label="消防工艺" align="center" prop="fireControl" /-->
      <el-table-column label="物联标记" align="center" prop="iotMark" />
      <!--el-table-column label="长度" align="center" prop="length" />
      <el-table-column label="宽度" align="center" prop="width" />
      <el-table-column label="高度" align="center" prop="hight" />
      <el-table-column label="面积" align="center" prop="area" /-->
      <el-table-column label="地址" align="center" prop="address" width="240" :show-overflow-tooltip="true" />
      <el-table-column label="备注" align="center" prop="remark" width="180" :show-overflow-tooltip="true" />
      <el-table-column label="创建者" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改仓位信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="名字" prop="name">
          <el-input v-model="form.name" placeholder="请输入名字" clearable />
        </el-form-item>
        <el-form-item label="父ID" prop="parentId">
          <el-input v-model="form.parentId" placeholder="请输入父ID" disabled />
        </el-form-item>
        <el-form-item label="显示顺序" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入显示顺序" clearable />
        </el-form-item>
        <el-form-item label="级别标记" prop="levelMark">
          <el-select v-model="form.levelMark" placeholder="请选择级别标记" style="width: 340px;" clearable>
            <el-option
              v-for="dict in dict.type.level_mark"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="仓库类型" prop="warehouseType">
          <el-select v-model="form.warehouseType" placeholder="请选择仓库类型" style="width: 340px;" clearable>
            <el-option
              v-for="dict in dict.type.warehouse_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="货物类型" prop="goodsType">
          <el-select v-model="form.goodsType" placeholder="请选择货物类型" style="width: 340px;" clearable>
            <el-option
              v-for="dict in dict.type.goods_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="使用状态" prop="useStatus">
          <el-select v-model="form.useStatus" placeholder="请选择使用状态" style="width: 340px;" clearable>
            <el-option
              v-for="dict in dict.type.use_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入编码" clearable />
        </el-form-item>
        <!--el-form-item label="3PL编码" prop="threeplCode">
          <el-input v-model="form.threeplCode" placeholder="请输入3PL编码" clearable />
        </el-form-item>
        <el-form-item label="天车操作" prop="craneOperateMode">
          <el-select v-model="form.craneOperateMode" placeholder="请选择天车操作" style="width: 340px;" clearable>
            <el-option
              v-for="dict in dict.type.crane_operate_mode"
              :key="dict.value"
              :label="dict.label"
              :value="dict.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="消防工艺" prop="fireControl">
          <el-select v-model="form.fireControl" placeholder="请选择消防工艺" style="width: 340px;" clearable>
            <el-option
              v-for="dict in dict.type.fire_control"
              :key="dict.value"
              :label="dict.label"
              :value="dict.label"
            ></el-option>
          </el-select>
        </el-form-item-->
        <el-form-item label="物联标记" prop="iotMark">
          <el-select v-model="form.iotMark" placeholder="请选择物联标记" style="width: 340px;" clearable>
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <!--el-form-item label="长度" prop="length">
          <el-input v-model="form.length" placeholder="请输入长度" clearable />
        </el-form-item>
        <el-form-item label="宽度" prop="width">
          <el-input v-model="form.width" placeholder="请输入宽度" clearable />
        </el-form-item>
        <el-form-item label="高度" prop="hight">
          <el-input v-model="form.hight" placeholder="请输入高度" clearable />
        </el-form-item>
        <el-form-item label="面积" prop="area">
          <el-input v-model="form.area" placeholder="请输入面积" clearable />
        </el-form-item-->
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" clearable />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLocate, getLocate, delLocate, addLocate, updateLocate } from "@/api/locates/locate";

export default {
  name: "Locate",
  dicts: ['warehouse_type', 'crane_operate_mode', 'sys_yes_no', 'use_status', 'level_mark', 'fire_control', 'goods_type'],
  data() {
    return {
      // 定义列表高度
      tableHeight: 430,
      // 定义查询高度
      queryHeight: 200,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仓位信息表格数据
      locateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 备注时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        parentId: null,
        ancestors: null,
        orderNum: null,
        levelMark: null,
        warehouseType: null,
        goodsType: null,
        useStatus: null,
        code: null,
        threeplCode: null,
        craneOperateMode: null,
        fireControl: null,
        iotMark: null,
        length: null,
        width: null,
        hight: null,
        area: null,
        address: null,
        remark: null,
        createBy: null,
        createTime: null,
      },
      // 表单参数
      form: {},
      id: '',
      level: 0,
      name: '',
      // 表单校验
      rules: {
        name: [
          { required: true, message: "名字不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
//    console.log('this.$route.query.id', this.$route.query.id);
//    this.id = this.$route.query.id;
//    this.name = this.$route.query.name;
//    this.queryParams.parentId = this.$route.query.id;
    if (this.$route.query.level === undefined) {
      this.level = '0';
    } else {
      this.level = this.$route.query.level;
      //console.log('this.$route.query.level', this.$route.query.level);
    }
    this.getList();
  },
  mounted() {
    this.$nextTick(() =>{
      // 根据浏览器高度设置初始高度 - 按钮行高度28 - 页码行高度28 - （面包屑+TAB行）高度160
      this.queryHeight = this.$refs.queryForm.$el.offsetHeight;
      this.tableHeight = window.innerHeight - this.$refs.queryForm.$el.offsetHeight - 28 - 28 - 160;
      // 监听浏览器高度变化，改变表格高度
      window.onresize = () =>{
        this.tableHeight = window.innerHeight - this.$refs.queryForm.$el.offsetHeight - 28 - 28 - 160;
      }
    })
  },
  methods: {
    /** 查询仓位信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      if (this.$route.query.id === undefined) {
        this.queryParams.parentId = '0';
      } else {
        this.queryParams.parentId = this.$route.query.id;
//        console.log('this.queryParams.parentId', this.queryParams.parentId);
      }
      listLocate(this.queryParams).then(response => {
        this.locateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        locateId: null,
        name: null,
        parentId: null,
        ancestors: null,
        orderNum: null,
        levelMark: null,
        warehouseType: null,
        goodsType: null,
        useStatus: null,
        code: null,
        threeplCode: null,
        craneOperateMode: null,
        fireControl: null,
        iotMark: null,
        length: null,
        width: null,
        hight: null,
        area: null,
        address: null,
        remark: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.locateId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      if (this.$route.query.level === undefined) {
        this.form.levelMark = '0';
        this.form.parentId = 0;
      } else {
        this.form.levelMark = this.$route.query.level;
        this.form.parentId = this.$route.query.id;
//        console.log('this.$route.query.level', this.$route.query.level);
      }
      if (this.$route.query.ancestors === undefined) {
        this.form.ancestors = '0';
      } else {
        if(this.$route.query.ancestors == '0'){
          this.form.ancestors = this.$route.query.id;
        }else {
          this.form.ancestors = this.$route.query.ancestors + ',' + this.$route.query.id;
        }
//        console.log('this.$route.query.ancestors', this.$route.query.ancestors);
//        console.log('this.form.ancestors', this.form.ancestors);
      }
      this.open = true;
      this.title = "添加仓位信息";
      this.form.useStatus = this.dict.type.use_status[0].label; //'使用';
//      console.log('this.dict.type.use_status[0].label', this.dict.type.use_status[1].label);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const locateId = row.locateId || this.ids
      getLocate(locateId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改仓位信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.locateId != null) {
            updateLocate(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLocate(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const locateIds = row.locateId || this.ids;
      this.$modal.confirm('是否确认删除仓位信息编号为"' + locateIds + '"的数据项？').then(function() {
        return delLocate(locateIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('locates/locate/export', {
        ...this.queryParams
      }, `locate_${new Date().getTime()}.xlsx`)
    },
    computeTableHeight(){
      if( this.showSearch==true ){
        this.tableHeight = window.innerHeight - this.queryHeight - 28 - 28 - 160;
      } else {
        this.tableHeight = window.innerHeight - 28 - 28 - 160;
      }
    },
    /** 仓库管理按钮操作 */
    handleWarehousManage(row) {
      const warehouseLocateIds = row.locateId || this.ids;

//      console.log('handleWarehousManage, warehouseLocateIds ', warehouseLocateIds);
      this.$router.push({
        path: '/warehouseManage/index/' + warehouseLocateIds,
        query: {
          id: warehouseLocateIds,
          level: 1,
          ancestors: row.ancestors,
          name: '仓库管理' + row.name
        }
      })     
    },
    /** 区位管理按钮操作 */
    handleRegionManage(row) {
      const warehouseLocateIds = row.locateId || this.ids;
 
//      console.log('handleRegionManage, warehouseLocateIds ', warehouseLocateIds);
      this.$router.push({
        path: '/regionManage/index/' + warehouseLocateIds,
        query: {
          id: warehouseLocateIds,
          level: 2,
          ancestors: row.ancestors,
          name: '区位管理' + row.name
        }
      })     
    },    
    /** 货位管理按钮操作 */
    handleGoodsLocateManage(row) {
      const warehouseLocateIds = row.locateId || this.ids;
 
//      console.log('handleGoodsLocateManage, warehouseLocateIds ', warehouseLocateIds);
      this.$router.push({
        path: '/goodsLocateManage/index/' + warehouseLocateIds,
        query: {
          id: warehouseLocateIds,
          level: 3,
          ancestors: row.ancestors,
          name: '货位管理' + row.name
        }
      })     
    },
    // 返回按钮
    handleClose() {
      // 关闭当前标签页（如果有）
      this.$tab.closePage();

      // 返回上一页面
      this.$router.back();
      // 或者使用 window.history.back();
    },
  }
};
</script>
